import BaseIndex from './baseIndex';
import { Common } from "UtilsCommon";

export default class ComplexDataGrid extends BaseIndex {
    remove(props, action) {
        if (!action.parameters) this.initRemoveAction(props, action);

        const { complexDataGridView } = action.parameters;

        complexDataGridView.remove(props.property.dataId);
    }

    initRemoveAction(props, action) {
        const { pageAxis } = props;
        const complexDataGridView = pageAxis.getProperty(action.complexDataGridView);

        action.parameters = { complexDataGridView };
    }

    dialogSelectRowData(props, action) {
        if (!action.parameters) this.initDialogSelectRowData(props, action);
        const { pageAxis, property } = props;
        const { dialogView } = action.parameters;

        const complexDataGridView = property.getView();

        complexDataGridView.selectRowData = property.data;
        complexDataGridView.selectRowDataAction = action;

        dialogView.expandSetValue && dialogView.expandSetValue(property.data);
        pageAxis.currentComplexDataGridView = complexDataGridView;

        this.showdialog(action, pageAxis, dialogView);
    }

    initDialogSelectRowData(props, action) {
        const { pageAxis } = props;

        const dialogView = Common.arrayFirst(pageAxis.pageConfig.dialogViews, (f) => f.name === action.dialogView);

        action.parameters = { dialogView };
    }

    dialogSelectRowDataList(props, action) {
        if (!action.parameters) this.initDialogSelectRowDataList(props, action);
        const { pageAxis, dataList } = props;
        const { dialogView, dataGridView } = action.parameters;

        const onOk = (e, p) => this.setSelectDataListDialogOk(e, p, props, action);

        action.selectDatas = {};

        dataGridView.setSelectDataList = (selectedRowKeys, dataList, primaryKey) => {
            const { pageIndex } = dataGridView.getPageInfo();
            const list = [];
            dataList.forEach(d => {
                if (selectedRowKeys.includes(d[primaryKey])) list.push(d);
            });
            action.selectDatas[pageIndex] = { dataList, selectDataList: list };
        };

        const primaryKey = dataGridView.entityPrimaryKey || dataGridView.entity.primaryKey;

        const ids = dataList.map(d => d[primaryKey]);

        this.showdialog(action, pageAxis, dialogView, onOk, () => {
            dataGridView.rowSelectedIds = ids;
            dataGridView.refresh && dataGridView.refresh(true);
        });
    }

    setSelectDataListDialogOk(e, p, props, action) {
        const { setDataList, dataList } = props;
        const { dataGridView } = action.parameters;

        const primaryKey = dataGridView.entityPrimaryKey || dataGridView.entity.primaryKey;

        //当前选择的数据行集合
        let selectDataList = [];
        //加载过的数据行集合
        let loadDataList = [];
        for (const key in action.selectDatas) {
            selectDataList = selectDataList.concat(action.selectDatas[key].selectDataList);
            loadDataList = loadDataList.concat(action.selectDatas[key].dataList);
        }

        //没有出现在加载过的里的数据行集合（复杂对象数据列表中的数据）
        dataList.forEach(d => {
            const item = Common.arrayFirst(loadDataList, f => f[primaryKey] === d[primaryKey]);
            if (item === null) selectDataList.push(d);

            const selectItem = Common.arrayFirst(selectDataList, f => f[primaryKey] === d[primaryKey]);
            if (selectItem !== null) for (const key in d) selectItem[key] = d[key];
        });

        setDataList(selectDataList);

        action.modalDialog.setIsVisible(false);
    }

    initDialogSelectRowDataList(props, action) {
        const { pageAxis } = props;

        const dialogView = Common.arrayFirst(pageAxis.pageConfig.dialogViews, (f) => f.name === action.dialogView);
        const dataGridView = pageAxis.getViewProperty(dialogView, action.dataGridView);

        action.parameters = { dialogView, dataGridView };
    }

    //保存实体数据，包含增加与更新
    saveEntityData(props, action) {
        const { editView, complexDataGrid, expandSetEntityData } = action.parameters;
        const { property } = props;
        const primaryKey = complexDataGrid.primaryKey || 'id';

        const entityData = this.getPropertyEntityData(props, editView, expandSetEntityData);
        if (entityData === false) return;

        const dataList = complexDataGrid.getValue();

        if (property.data) {
            entityData[primaryKey] = property.data[primaryKey];
            const list = [];
            for (let i = 0; i < dataList.length; i++) {
                const item = dataList[i];
                if (Common.isEquals(item[primaryKey], entityData[primaryKey])) {
                    for (const key in entityData) {
                        item[key] = entityData[key];
                    }
                }
                list.push(item);
            }
            complexDataGrid.setValue(list);
        }
        else {
            if (!entityData[primaryKey]) entityData[primaryKey] = Common.createGuid();
            const list = dataList.map(m => m);
            list.push(entityData);
            complexDataGrid.setValue(list);
        }

        action.successCallback && action.successCallback();
    }

    getPropertyEntityData(props, view, expandSetEntityData) {
        const { pageAxis } = props;
        const { defaultEditData } = view;

        let entityData = this.getPropertyPropertiesValue(view.properties, pageAxis);

        if (expandSetEntityData) entityData = expandSetEntityData({ entityData, props, view });

        if (entityData === false) return false;

        //设置默认编辑数据
        if (defaultEditData) for (const key in defaultEditData) entityData[key] = defaultEditData[key];

        return entityData;
    }
}